package phone.example.com.a36kr.adapter.newslistviewadapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;

import phone.example.com.a36kr.bean.NewsBean;
import phone.example.com.a36kr.R;
import phone.example.com.a36kr.bean.UrlBean;
import phone.example.com.a36kr.nettool.NetTool;

/**
 * Created by dllo on 16/8/15.
 */
public class NewsAdapter extends BaseAdapter {

    private Context mContext;
    private NewsBean newsBean;
    private final SimpleDateFormat mformat;
    // newsFragment传过来的网址 对比网址显示columnNameTV用的
    private String newsReplaceUrl = UrlBean.NEWS_ALL_URL;

    public void setNewsReplaceUrl(String newsReplaceUrl) {
        this.newsReplaceUrl = newsReplaceUrl;
        notifyDataSetChanged();
    }

    public NewsAdapter(Context mContext) {
        this.mContext = mContext;
        mformat = new SimpleDateFormat("HH:mm");

    }


    public void setNewsBean(NewsBean newsBean) {
        this.newsBean = newsBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return newsBean != null ? newsBean.getData().getData().size() : 0;
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.news_item_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.titleTV.setText(newsBean.getData().getData().get(position).getTitle());
        viewHolder.nameTV.setText(newsBean.getData().getData().get(position).getUser().getName());
        NetTool netTool = new NetTool();
        netTool.getImage(newsBean.getData().getData().get(position).getFeatureImg(), viewHolder.imageView);
        // 判断发送时间
        Long now = System.currentTimeMillis();
        long dTime = now - Long.valueOf(newsBean.getData().getData().get(position).getPublishTime());
        // Log.d("NewsAdapter", "newsBean.getData().getData().get(position).getPublishTime():" + newsBean.getData().getData().get(position).getPublishTime());
        if (dTime > 1000 * 60 * 60) {
            //显示正常日期
            String time = mformat.format(Long.valueOf(dTime));
            viewHolder.publishTimeTV.setText(time);
        } else if (dTime > 1000 * 60) {
            //显示多长分钟之前
            String min = String.valueOf((int) dTime / 1000 / 60);
            viewHolder.publishTimeTV.setText(min + "分钟之前");
        } else {
            //显示多少s之前
            String second = String.valueOf((int) dTime / 1000);
            viewHolder.publishTimeTV.setText(second + "s之前");
        }

        //判断在哪个抽屉标签里, 如果不是全部  隐藏掉发布标签
        if (UrlBean.NEWS_ALL_URL.equals(newsReplaceUrl)) {
            int num = Integer.parseInt(newsBean.getData().getData().get(position).getColumnId());
            if (num == 69) {//资本
                viewHolder.columnNameTV.setTextColor(Color.rgb(0, 0, 255));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 23) {//大公司
                viewHolder.columnNameTV.setTextColor(Color.rgb(135, 206, 250));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 72) {//其他
                viewHolder.columnNameTV.setTextColor(Color.rgb(0, 0, 255));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 67) {//早期项目
                viewHolder.columnNameTV.setTextColor(Color.rgb(0, 250, 154));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 68) {//B轮后
                viewHolder.columnNameTV.setTextColor(Color.rgb(0, 191, 255));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 70) {//深度
                viewHolder.columnNameTV.setTextColor(Color.rgb(255, 105, 180));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            } else if (num == 71) {//研究
                viewHolder.columnNameTV.setTextColor(Color.rgb(255, 165, 0));
                viewHolder.columnNameTV.setText(newsBean.getData().getData().get(position).getColumnName());
            }

        } else {
            viewHolder.columnNameTV.setText(null);
        }



        return convertView;
    }

    private class ViewHolder {


        private final TextView titleTV;
        private final ImageView imageView;
        private final TextView nameTV;
        private final TextView publishTimeTV;
        private final TextView columnNameTV;

        public ViewHolder(View convertView) {
            imageView = (ImageView) convertView.findViewById(R.id.items_news_featureImg);
            titleTV = (TextView) convertView.findViewById(R.id.items_news_titleTV);
            nameTV = (TextView) convertView.findViewById(R.id.items_news_nameTV);
            publishTimeTV = (TextView) convertView.findViewById(R.id.items_news_publishTimeTV);
            columnNameTV = (TextView) convertView.findViewById(R.id.items_news_columnNameTV);

        }
    }


}
